package com.gg.fmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gg.fmallspringboot.entity.UserAddr;
import com.gg.fmallspringboot.mapper.UserAddrMapper;
import com.gg.fmallspringboot.mapper.UserMapper;


import com.gg.fmallspringboot.entity.User;
import com.gg.fmallspringboot.model.Result;
import com.gg.fmallspringboot.service.UserService;
import com.gg.fmallspringboot.utils.MD5Util;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author wx
 * @date 2021-06-18 19:12:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserAddrMapper userAddrMapper;

    @Override
    public Result<User> check(String userName, String password) {
        User user = userMapper.queryUser(userName);
        if (user != null) {
            String md5Pwd = MD5Util.md5(password);
            if (md5Pwd.equals(user.getPassword())) {
                JwtBuilder jwtBuilder = Jwts.builder();
                String token = jwtBuilder.setSubject(userName)   //主题，就是token中携带的数据
                        .setIssuedAt(new Date())    //设置token的生成时间
                        .setId(user.getUserId() + "")  //设置用户id为token  id
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))   //设置过期时间
                        .signWith(SignatureAlgorithm.HS256, "QIANfeng666")    //设置加密方式和加密密码
                        .compact();

                return new Result(1000, token, user);
            } else {
                return new Result(1001, "密码错误", null);
            }
        } else {
            return new Result(1002, "用户名不存在", null);
        }
    }

    @Override
    public Result checkUserName(String userName) {
        User user = userMapper.queryUser(userName);
        if (user != null) {
            return new Result(1000, "用户名重复", null);
        } else {
            return new Result(1001, "用户名可以使用", null);
        }
    }

    @Override
    public Result insertUser(String userName, String password) {
        String s = MD5Util.md5(password);
        String userImg = "img/default.png";
        int i = userMapper.insertUser(userName, s,userImg);
        if (i > 0) {
            return new Result(1000, "注册成功", null);
        } else {
            return new Result(1001, "注册失败", null);
        }
    }

    @Override
    public Result<UserAddr> getAllById(Integer userId) {
        QueryWrapper<UserAddr> qw = new QueryWrapper();
        qw.eq("user_id",userId);
        List<UserAddr> userAddrList = userAddrMapper.selectList(qw);
        Result result;
        if (userAddrList==null){
            result = new Result(1,"查询失败");
        }else {
            result = new Result(0,"查询成功",userAddrList);
        }
        return result;
    }

    @Override
    public Result addUserAddr(UserAddr userAddr) {
        int i = userAddrMapper.addUserAddr(userAddr);
        Result result;
        if (i<=0){
            result = new Result(1,"添加失败");
        }else {
            result = new Result(0,"添加成功");
        }
        return result;
    }

    @Override
    public Result updateUserAddr(UserAddr userAddr) {

        int i = userAddrMapper.updateUserAddr(userAddr);
        Result result;
        if (i<=0){
            result = new Result(1,"修改失败");
        }else {
            result = new Result(0,"修改成功");
        }
        return result;
    }

    @Override
    public Result deleteUserAddr(Integer addrId) {
        int i = userAddrMapper.deleteUserAddr(addrId);
        Result result;
        if (i<=0){
            result = new Result(1," 删除失败");
        }else {
            result = new Result(0,"删除成功");
        }
        return result;
    }
}
